#include "Common/TableDataEx/ChatCfg.h"
#include "Common/SceneDefine.h"
#include "base/core/random.h"
#include "base/core/char_operation.h"
#include "Common/TableData/ChatSpecialchatCfg.h"

ChatCfg::ChatCfg()
{
	m_mapChatSpecial.clear();
}

ChatCfg::~ChatCfg()
{
}

bool ChatCfg::Init()
{
	if (!Process())
	{
		return false;
	}
	return true;
}

void ChatCfg::UnInit()
{
	m_mapChatSpecial.clear();
}

int32_t ChatCfg::ChatIDByEventType(int32_t eventType, int32_t param)
{
	const SChatSpecialCfg *pSpecialCfg = GetChatSpecialCfg(eventType);
	if (nullptr == pSpecialCfg)
	{
		return 0;
	}
	SET_INT32::const_iterator iter = pSpecialCfg->setParam.find(param);
	if (iter != pSpecialCfg->setParam.end())
	{
		return pSpecialCfg->chatId;
	}
	return 0;
}

const SChatSpecialCfg *ChatCfg::GetChatSpecialCfg(int32_t eventType)
{
	MapChatSpecialCfg::iterator iter = m_mapChatSpecial.find(eventType);
	return (iter != m_mapChatSpecial.end()) ? &iter->second : nullptr;
}

bool ChatCfg::Process()
{

	const ChatSpecialchatCfgMap *pChatMap = g_GetChatSpecialchatCfgTable()->GetChatSpecialchatCfgMap();
	if (nullptr != pChatMap)
	{
		ChatSpecialchatCfgMap::const_iterator iter = pChatMap->begin();
		for (; iter != pChatMap->end(); ++iter)
		{
			const ChatSpecialchatCfgInfo &info = iter->second;
			MapChatSpecialCfg::iterator iterType = m_mapChatSpecial.find(info.type);
			if (iterType != m_mapChatSpecial.end())
			{
				LogErrFmtPrint("[common] ChatCfg::Process...repeated data....id:%d,type:%d ", info.id, info.type);
				return false;
			}
			SChatSpecialCfg cfg;
			cfg.chatId = info.chat_Id;
			cfg.eventType = info.type;
			CommonApi::SplitStrToSetInt(info.param, ",", &cfg.setParam);
			m_mapChatSpecial[cfg.eventType] = cfg;
		}
	}

	return true;
}